package com.example.a80205050.myapplication.MyActivity;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.icu.util.Calendar;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import com.example.a80205050.myapplication.Dao.OutaccountDao;
import com.example.a80205050.myapplication.Modle.Tb_outaccount;

/**
 * Created by 80205050 on 2017/10/31.
 */
public class AddOutaccount extends Activity {
    protected static final int DATE_DIALOG_ID=0;//创建气日期对话框常量
    EditText txtMoney,txtTime,txtAddress,txtMark;//四个编辑框的对象
    Spinner spType;//类别的是spinner对象
    Button btnSave,btnCancel;//取消和保存按钮
    private int mYear;//年
    private int mMonth;//月
    private int mDay;//日
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addoutaccount);  //设置布局文件
        txtMoney=(EditText)findViewById(R.id.txtMoney);//获取输入金额的编辑框
        txtTime=(EditText)findViewById(R.id.txtTime);//获取输入时间的编辑框
        if(txtTime==null){
           Log.i("AddOutaccount","txtTimeNULL");
        }
        txtAddress=(EditText)findViewById(R.id.txtAddress);//获取地址的编辑框
        txtMark=(EditText)findViewById(R.id.txtMark);//获取备注的编辑框
        btnCancel=(Button)findViewById(R.id.btnCancel); //获取取消的按钮
        btnSave=(Button)findViewById(R.id.btnSave); //获取保存的按钮
        spType = (Spinner) findViewById(R.id.spType);// 获取类别下拉列表
        txtTime.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog(DATE_DIALOG_ID);
            }
        });
        btnSave.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                String strInMoney=txtMoney.getText().toString();//获取输入的金额
                if(!strInMoney.isEmpty()){
                    OutaccountDao outaccountDao=new OutaccountDao(AddOutaccount.this);//获取数据库操作的对象
                    //生成收入的实体类对象
                    Tb_outaccount tb_outaccount=new Tb_outaccount(outaccountDao.getMaxId()+1,
                            Double.parseDouble(strInMoney),
                            txtTime.getText().toString(),
                            spType.getSelectedItem().toString(),
                            txtAddress.getText().toString(),
                            txtMark.getText().toString());
                    outaccountDao.add(tb_outaccount);//添加收入信息到数据库
                    Toast.makeText(AddOutaccount.this,"新增支出数据添加到数据库成功！",Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(AddOutaccount.this,"请输入支出金额",Toast.LENGTH_LONG).show();
                }

            }
        });

        btnCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                txtMoney.setText(" ");//设置金额文本为空
                txtMoney.setHint("0.00");//设置金额文本框的提示
                txtTime.setText(" ");//设置时间文本框为空
                txtTime.setHint("2010-01-01");//设置时间提示框的提示
                txtAddress.setText(" ");//设置付款文本框为空
                txtMark.setText(" ");//设置备注的文本框为空
                spType.setSelection(0);//默认选择第一项
            }
        });
        final Calendar calendar=Calendar.getInstance();//获取系统当前的日期
        mYear=calendar.get(Calendar.YEAR);//获取年份
        mMonth=calendar.get(Calendar.MONTH);//获取月份
        mDay=calendar.get(Calendar.DATE);//获取天数
        updateDisplay();
    }
    private  DatePickerDialog.OnDateSetListener mDataSetListener=new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
            mYear=i;
            mMonth=i1;
            mDay=i2;
        }
    };
    @Nullable
    @Override
    protected Dialog onCreateDialog(int id, Bundle args) {
        switch (id){
            case DATE_DIALOG_ID:
                return new DatePickerDialog(this,mDataSetListener,mYear,mMonth,mDay);
        }
        return null;
    }
    //更新当前系统的中显示的时间
    private void updateDisplay(){
        txtTime.setText(new StringBuilder().append(mYear)
                .append("-").append(mMonth+1).append("-").append(mDay));
    }
}
